﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dancer_Partner_Matching
{
    public class PartnerMatcher
    {
        public void Matching(List<Dancer> males)
        {
            var freeMaleCount = males.Count;
            while (freeMaleCount > 0)
            {
                foreach (var male in males)
                {
                    if (male.Partner == null)//如果舞者尚未有舞伴
                    {
                        //则从偏好列表中选择尚未有舞伴
                        var female = male.GetPartnerInPrefs();
                        if (female.Partner == null)
                        {
                            male.SetPartner(female);
                            freeMaleCount--;
                        }
                        else if (female.IsPrefers(male))
                        {
                            male.SetPartner(female);
                        }
                    }
                }
            }

        }

        /// <summary>
        /// 检查匹配舞伴是否平衡
        /// </summary>
        /// <param name="males"></param>
        /// <returns></returns>
        public bool IsStable(List<Dancer> males, List<Dancer> females)
        {
            foreach (var male in males)
            {
                foreach (var female in females)
                {
                    if (male.IsPrefers(female) && female.IsPrefers(male))
                        return false;
                }
            }

            return true;
        }
    }
}
